﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using Newtonsoft.Json;
using System.Configuration;
using System.Data;

namespace PromotionEngine
{
    public partial class Item_Property_Link : System.Web.UI.Page
    {
        public class Response
        {
            public List<Model.ItemPropertyLink> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public Response(List<Model.ItemPropertyLink> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public Response(string errors)
            {
                this.Errors = errors;
            }

            public Response()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string itemID = Request.QueryString["ItemID"];
            string itemCode = Request.QueryString["ItemCode"];
            string itemName = Request.QueryString["ItemName"];

            this.lblItemCode.Text = itemCode;
            this.lblItemName.Text = itemName;
            this.lblItemID.Value = itemID;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetItemPropertyLinks()
        {
            List<Model.ItemPropertyLink> lstItemProperty = new List<Model.ItemPropertyLink>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllCustomerPropertyLink(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString());
            if (dsPromotion != null)
            {

                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.ItemPropertyLink itemPropertyLink = new Model.ItemPropertyLink();
                    itemPropertyLink.PropertyLinkID = r["PropertyLinkID"].ToString();
                    itemPropertyLink.ItemID = r["ItemID"].ToString();
                    itemPropertyLink.ItemCode = r["ItemCode"].ToString();
                    itemPropertyLink.ItemName = r["ItemName"].ToString();
                    itemPropertyLink.PropertyDetailID = r["PropertyDetailID"].ToString();
                    itemPropertyLink.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    itemPropertyLink.PropertyDetailName = r["PropertyDetailName"].ToString();
                    lstItemProperty.Add(itemPropertyLink);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstItemProperty };
                return new Response(lstItemProperty, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateItemPropertyLink(string oItemPropertyLink, bool isInsert)
        {
            Model.ItemPropertyLink itemPropertyLink = JsonConvert.DeserializeObject<Model.ItemPropertyLink>(oItemPropertyLink);

            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonItemPropertyLink(itemPropertyLink.ItemID, itemPropertyLink.PropertyDetailID, itemPropertyLink.PropertyLinkID,
                  ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetItemPropertyLinkByItem(string itemID, string propertyID)
        {
            List<Model.ItemPropertyLink> lstItemProperty = new List<Model.ItemPropertyLink>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllItemPropertyLinkByItem(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyID, itemID);
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.ItemPropertyLink itemProperty = new Model.ItemPropertyLink();
                    itemProperty.PropertyLinkID = r["ID"].ToString();
                    itemProperty.ItemID = r["ItemID"].ToString();
                    itemProperty.PropertyDetailID = r["PropertyDetailID"].ToString();
                    itemProperty.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    itemProperty.PropertyDetailName = r["PropertyDetailName"].ToString();
                    lstItemProperty.Add(itemProperty);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstItemProperty };
                return new Response(lstItemProperty, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateItemPropertyLinks(string oItemPropertyLink, bool isInsert)
        {
            List<Model.ItemPropertyLink> itemPropertyLinks = JsonConvert.DeserializeObject<List<Model.ItemPropertyLink>>(oItemPropertyLink);

            Promotion pmt = new Promotion();
            foreach (Model.ItemPropertyLink customerPropertyLink in itemPropertyLinks)
            {
                string errMsg = pmt.UpdateJsonItemPropertyLink(customerPropertyLink.ItemID, customerPropertyLink.PropertyDetailID, customerPropertyLink.PropertyLinkID,
                      ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
                if (errMsg.Length == 0)
                {
                    //Show error message
                }
            }
        }
    }
}
